package 算法.leetcode.offer.nk;

import 算法.leetcode.offer.base.ListNode;

/**
 * @author lchenglong
 * @date 2022/3/26
 */
public class BM16 {

    public static void main(String[] args) {
        BM16 instance = new BM16();
        ListNode head = ListNode.init(new int[]{1, 1,2, 2,3,3});
        ListNode node = instance.deleteDuplicates(head);
        ListNode.print(node);
    }
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        // 开始边界
        if(head == null || head.next == null)return head;
        ListNode cur = head,vhead = new ListNode(0), pre = vhead;
        vhead.next = head;
        int flag = 0;
        while(cur!=null){
            while(cur.next!=null){
                if(cur.val == cur.next.val){
                    cur = cur.next;
                    flag = 1;
                } else {
                    if(flag ==1)pre.next = cur.next;
                    break;
                }
            }
            cur = cur.next;
            if(flag==0)pre = pre.next;
            // 退出边界
            if(cur==null && flag==1)pre.next = cur;
            flag=0;
        }
        return vhead.next;
    }
}
